package control;

import database.Database;
import java.sql.SQLException;
import model.Product;
import utility.Converter;

/**
 * Denne klasses opgave er at manipulere i databasen i forbindelse med produkt
 * @author Sonny Sandberg
 */
public abstract class ProductControl 
{
    /**
     * Denne metode kan håndtere både tilføjelse og ændring af et produkt
     * @param Product
     * @return int
     * @throws ClassNotFoundException
     * @throws SQLException 
     */
    public static int addAlterProduct(Product product) throws ClassNotFoundException, SQLException
    {
        Database db = new Database();
        
        int productID = product.getId();
        String productName = product.getName();
        
        String productSize = product.getSize();
        int productFamilyID = product.getProductFamily().getId();
        double productPurchasePrice = product.getPurchasePrice();
        double productSalesPrice = product.getSalesPrice();
        int productHasDecoration = Converter.booleanToInt(product.isHasDecoration());
        int serviceProduct = Converter.booleanToInt(product.isRemainAfterSale());
        int productSold = Converter.booleanToInt(product.isSold());
        int productActive = Converter.booleanToInt(product.isActive());
        int handled = 0;
        
        // Hvis der er et id på produktet, skal det opdateres
        if (product.getId() > 0)
        {
            handled = db.manipulateDB(""
                    + "UPDATE bs_product SET"
                    + " name = '"+productName+"', "
                    + " szd = '"+productSize+"', "
                    + " purchasing_price = "+productPurchasePrice+", "
                    + " sales_price = "+ productSalesPrice+", "
                    + " has_decoration = "+productHasDecoration+", "
                    + " remain_after_Sale = "+serviceProduct+", "
                    + " active = "+productActive+", "
                    + " sold = "+productSold+", "
                    + " fk_product_family = "+productFamilyID
                    + " WHERE id = "+productID);
        }
        // Ellers skal det tilføjes
        else
        {
            handled = db.manipulateDB(""
                    + "INSERT INTO bs_product "
                    + " (name, szd, purchasing_price, sales_price, has_decoration, remain_after_sale, fk_product_family) "
                    + "VALUES "
                    + "("
                    + "'"+productName+"', "
                    + "'"+productSize+"', "
                    + productPurchasePrice + ", "
                    + productSalesPrice + ", "
                    + productHasDecoration + ", "
                    + serviceProduct + ", "
                    + productFamilyID
                    + ")");
        }
        db.closeDB();
        return handled;
    }
    
    /**
     * Bruges til at slette et produkt. Der er indbygget i SQL strengen, at et produkt der er solgt, ikke kan slettes.
     * Dette ville være umuliggjort at databasen selv, men denne ekstra foranstaltning sikrer mod en Exception bliver kastet, på grund af child/parentforhold.
     * @param Product product
     * @return int
     * @throws ClassNotFoundException
     * @throws SQLException 
     */
    public static int deleteProduct(Product product) throws ClassNotFoundException, SQLException
    {
        int deleted = 0;
        
        if (product.isSold() == false)
        {
            Database db = new Database();
            
            deleted = db.manipulateDB("DELETE FROM bs_product WHERE id = '"+product.getId()+"' AND sold = 0");
            
            db.closeDB();            
        }
        else
        {
            // Trying to delete a product, that has been sold. This is not possible
        }
        
        return deleted;
    }
    
    /**
     * Sæt produktet solgt eller ikke solgt
     * @param Product
     * @param boolean
     * @throws ClassNotFoundException
     * @throws SQLException 
     */
    public static void setProductSold(Product product, boolean soldOrNot) throws ClassNotFoundException, SQLException
    {
        Database db = new Database();
        
        int sold = utility.Converter.booleanToInt(soldOrNot);
        db.manipulateDB("UPDATE bs_product SET sold = "+sold+" WHERE id = '"+product.getId()+"'");

        db.closeDB();
    }
}